package com.webwork.lims.statistics.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.orders.dataobject.Order;
import com.webwork.lims.orders.dataobject.OrderStatus;
import com.webwork.lims.orders.service.OrderService;
import com.webwork.lims.transport.dataobject.Zone;
import com.webwork.lims.transport.service.ZoneService;
import com.webwork.lims.util.DateFormat;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/statistics")
public class StatisticsController extends SuperCtrl {

	@Autowired
	private OrderService orderSve;
	
	@Autowired
	private ZoneService zoneSve;
	
	@RequestMapping(value = "/statisticsByTime")
	public String statisticsByTime() {
		return "statisticsByTime";
	}
	
	@RequestMapping(value = "/statisticsByZone")
	public String statisticsByZone() {
		return "statisticsByZone";
	}

	@Override
	public String mainPage() {
		return null;
	}
	
	@RequestMapping(value = "queryTimeStatistics")
	@ResponseBody
	public ResponseEntity<String> queryTimeStatistics(
			@RequestParam(required = false, value = "type") String type,
			@RequestParam(required = false, value = "startTime") long startTime,
			@RequestParam(required = false, value = "endTime") long endTime)
					throws Exception {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		Date date = new Date();
		String dateStr = "";
		if (type.equalsIgnoreCase("year")) {
			dateStr = DateFormat.YEAR.format(date);
		} else if (type.equalsIgnoreCase("month")) {
			dateStr = DateFormat.MONTH.format(date);
		} else if (type.equalsIgnoreCase("day")) {
			dateStr = DateFormat.DAY.format(date);
		} 
		
		Order queryObj = new Order();
		queryObj.setStatus(OrderStatus.COMPLETE);
		queryObj.setQueryTime(dateStr);
		int size = orderSve.count(queryObj);
		Map<String, String> map = new HashMap<String, String>();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		map.put("name", dateStr + "订单数");
		map.put("value", size + "");
		list.add(map);
		ret.put("total", 1);
		ret.put("rows", list);
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "queryTimeGraph")
	@ResponseBody
	public ResponseEntity<String> queryTimeGraph(
			@RequestParam(value = "type") String type,
			@RequestParam(value = "startTime") long startTime,
			@RequestParam(value = "endTime") long endTime)
					throws Exception {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		Order queryObj = new Order();
		queryObj.setStatus(OrderStatus.COMPLETE);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		String dateStr = "";
		if (type.equalsIgnoreCase("year")) {
			for (int i = 1; i < 13; i++) {
				dateStr = DateFormat.YEAR.format(date);
				if (i < 10) {
					dateStr += "-0" + i;
				} else {
					dateStr += "-" + i;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				queryObj.setQueryTime(dateStr);
				int size = orderSve.count(queryObj);
				map.put("name", i + "月");
				map.put("value", size);
				list.add(map);
			}
			
		} else if (type.equalsIgnoreCase("month")) {
			c.setTime(date);
			int cc = c.getActualMaximum(Calendar.DAY_OF_MONTH);
			for (int i = 1; i < cc + 1; i++) {
				dateStr = DateFormat.MONTH.format(date);
				if (i < 10) {
					dateStr += "-0" + i;
				} else {
					dateStr += "-" + i;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				queryObj.setQueryTime(dateStr);
				int size = orderSve.count(queryObj);
				map.put("name", i + "日");
				map.put("value", size);
				list.add(map);
			}
		} else if (type.equalsIgnoreCase("day")) {
			for (int i = 0; i < 24; i++) {
				dateStr = DateFormat.DAY.format(date);
				if (i < 10) {
					dateStr += " 0" + i;
				} else {
					dateStr += " " + i;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				queryObj.setQueryTime(dateStr);
				int size = orderSve.count(queryObj);
				map.put("name", i + "点");
				map.put("value", size);
				list.add(map);
			}
		} else {
			Date firstDate = orderSve.queryFirstDate();
			String firstYear = DateFormat.YEAR.format(firstDate);
			String nowYear = DateFormat.YEAR.format(date);
			do {
				Map<String, Object> map = new HashMap<String, Object>();
				queryObj.setQueryTime(firstYear);
				int size = orderSve.count(queryObj);
				map.put("name", firstYear + "年");
				map.put("value", size);
				list.add(map);
				
				c.setTime(firstDate);
				c.add(Calendar.YEAR, 1);
				firstDate = c.getTime();
				firstYear = DateFormat.YEAR.format(firstDate);
			} while (Integer.valueOf(firstYear) <= Integer.valueOf(nowYear));
		}
		
		ret.put("total", 1);
		ret.put("rows", list);
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "queryZoneStatistics")
	@ResponseBody
	public ResponseEntity<String> queryZoneStatistics(
			@RequestParam(required = false, value = "country") String country,
			@RequestParam(required = false, value = "province") String province,
			@RequestParam(required = false, value = "city") String city)
					throws Exception {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		int size = 0;
		
		Order orderQuery = new Order();
		orderQuery.setStatus(OrderStatus.COMPLETE);
		
		Zone queryObj = new Zone();
		queryObj.setCountry(country);
		queryObj.setProvince(province);
		queryObj.setCity(city);
		List<Zone> zones = zoneSve.query(queryObj);
		for (Zone zone : zones) {
			orderQuery.setZoneCode(zone.getZoneCode());
			size += orderSve.count(orderQuery);
		}
		
		Map<String, String> map = new HashMap<String, String>();
		String str = "";
		if (!StrManager.isEmpty(country)) {
			str += country;
		}
		if (!StrManager.isEmpty(province)) {
			str += province;
		}
		if (!StrManager.isEmpty(city)) {
			str += city;
		}
		if (StrManager.isEmpty(str)) {
			str = "全部地区";
		}
		List<Map<String, String>> test = new ArrayList<Map<String, String>>();
		map.put("name", str);
		map.put("value", size + "");
		test.add(map);
		ret.put("total", 1);
		ret.put("rows", test);
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "queryZoneGraph")
	@ResponseBody
	public ResponseEntity<String> queryZoneGraph(
			@RequestParam(value = "country") String country,
			@RequestParam(value = "province") String province,
			@RequestParam(value = "city") String city)
					throws Exception {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
	
		Order queryObj = new Order();
		queryObj.setStatus(OrderStatus.COMPLETE);
		
		Map<String, Object> ret = new HashMap<String, Object>();
		if (StrManager.isEmpty(country) && StrManager.isEmpty(province) && StrManager.isEmpty(city)) {
			int total = orderSve.count(queryObj);
			for (String countryStr : zoneSve.queryCountryByGroup()) {
				int size = 0;
				Zone zoneQuery = new Zone();
				zoneQuery.setCountry(countryStr);
				List<Zone> zones = zoneSve.query(zoneQuery);
				for (Zone zone : zones) {
					queryObj.setZoneCode(zone.getZoneCode());
					size += orderSve.count(queryObj);
				}
				float x = size;
				float y = total;
				ret.put(countryStr, x / y);
			}
		} else if (!StrManager.isEmpty(country) && StrManager.isEmpty(province) && StrManager.isEmpty(city)) {
			int total = 0;
			Zone zoneQuery = new Zone();
			zoneQuery.setCountry(country);
			List<Zone> zones = zoneSve.query(zoneQuery);
			for (Zone zone : zones) {
				queryObj.setZoneCode(zone.getZoneCode());
				total += orderSve.count(queryObj);
			}
			for (String provinceStr : zoneSve.queryProvinceByGroup(country)) {
				int size = 0;
				zoneQuery = new Zone();
				zoneQuery.setCountry(country);
				zoneQuery.setProvince(provinceStr);
				zones = zoneSve.query(zoneQuery);
				for (Zone zone : zones) {
					queryObj.setZoneCode(zone.getZoneCode());
					size += orderSve.count(queryObj);
				}
				float x = size;
				float y = total;
				ret.put(provinceStr, x / y);
			}
		} else if (!StrManager.isEmpty(province) && StrManager.isEmpty(city)) {
			int total = 0;
			Zone zoneQuery = new Zone();
			zoneQuery.setCountry(country);
			zoneQuery.setProvince(province);
			List<Zone> zones = zoneSve.query(zoneQuery);
			for (Zone zone : zones) {
				queryObj.setZoneCode(zone.getZoneCode());
				total += orderSve.count(queryObj);
			}
			for (String cityStr : zoneSve.queryCityByGroup(province)) {
				int size = 0;
				zoneQuery = new Zone();
				zoneQuery.setCountry(country);
				zoneQuery.setProvince(province);
				zoneQuery.setCity(cityStr);
				zones = zoneSve.query(zoneQuery);
				for (Zone zone : zones) {
					queryObj.setZoneCode(zone.getZoneCode());
					size += orderSve.count(queryObj);
				}
				float x = size;
				float y = total;
				ret.put(cityStr, x / y);
			}
		} else if (!StrManager.isEmpty(city)) {
			int total = 0;
			Zone zoneQuery = new Zone();
			zoneQuery.setCountry(country);
			zoneQuery.setProvince(province);
			zoneQuery.setCity(city);
			List<Zone> zones = zoneSve.query(zoneQuery);
			for (Zone zone : zones) {
				queryObj.setZoneCode(zone.getZoneCode());
				total += orderSve.count(queryObj);
			}
			for (Zone zone : zones) {
				for (String addressStr : orderSve.queryAddressByGroup(zone.getZoneCode())) {
					int size = 0;
					queryObj.setZoneCode(zone.getZoneCode());
					queryObj.setAddress(addressStr);
					size += orderSve.count(queryObj);
					float x = size;
					float y = total;
					ret.put(zone.getCountry() + "-" + zone.getProvince() + "-" + zone.getCity() + "-" + addressStr, x / y);
				}
			}
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
